package com.laundry.controller.admin;

import com.laundry.dto.ResultDTO;
import com.laundry.dto.StatisticsDTO;
import com.laundry.service.OrderService;
import com.laundry.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/admin")
public class AdminDashboardController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @GetMapping("/statistics")
    public ResultDTO getStatistics() {
        try {
            // 获取用户总数
            int userCount = userService.getAllUsers().size();
            
            // 获取订单总数
            int orderCount = orderService.getAllOrders().size();
            
            // 获取待处理订单数（状态为1-已支付待取件和2-已取件处理中的订单）
            List<Integer> pendingStatuses = Arrays.asList(1, 2);
            int pendingOrderCount = orderService.findByStatusIn(pendingStatuses).size();
            
            // 获取今日收入（状态为已支付的今日订单）
            BigDecimal todayIncome = orderService.calculateTodayIncome();
            
            // 创建统计数据对象
            StatisticsDTO statistics = new StatisticsDTO();
            statistics.setUserCount(userCount);
            statistics.setOrderCount(orderCount);
            statistics.setPendingOrderCount(pendingOrderCount);
            statistics.setTodayIncome(todayIncome);
            
            return ResultDTO.success(statistics);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取统计数据失败：" + e.getMessage());
        }
    }
} 